#include <xpam/ui/menu.h>
#include <xpam/ui/window_utils.h>
namespace xpam
{
    namespace ui
    {
        void menu::create()
        {
            wnd.create();
            _m = ::CreatePopupMenu();
        }

        void menu::destroy()
        {
            ::DestroyMenu((HMENU)_m);
            _m = 0;
            wnd.destroy();
        }

        int menu::show(const xpam::point<int> & pt)
        {
            return ::TrackPopupMenuEx((HMENU)_m, TPM_NONOTIFY | TPM_RETURNCMD | TPM_BOTTOMALIGN | TPM_RIGHTBUTTON, pt.x, pt.y, (HWND)wnd.get_internal_handle(), 0);
        }

        void menu::append(const std::string & text, int id)
        {
            MENUITEMINFOA mii;
            mii.cbSize = sizeof(MENUITEMINFO);
            mii.fMask = MIIM_STRING | MIIM_ID;
            mii.fType = MFT_STRING;
            mii.dwTypeData = (LPSTR)text.c_str();
            mii.wID = id;
            InsertMenuItemA((HMENU)_m, 0x79999999, TRUE, &mii);
            //AppendMenuA((HMENU)_m, MIIM_STRING | MIIM_ID, id, (LPSTR)text.c_str());
        }

        void menu::append_separator()
        {
            MENUITEMINFOA mii;
            mii.cbSize = sizeof(MENUITEMINFO);
            mii.fMask = MIIM_TYPE;
            mii.fType = MFT_SEPARATOR;
            mii.dwTypeData = "-";
            InsertMenuItemA((HMENU)_m, 0x79999999, TRUE, &mii);
        }
    }
}